/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.trafficando.presentation.view.feed;

import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.abdera.model.Feed;
import org.apache.abdera.writer.Writer;
import org.springframework.web.servlet.view.AbstractView;
import static com.appspot.trafficando.util.AtomHelper.*;
/**
 *
 * @author Domenico Maria Giffone
 */
public class AbstractAtomFeedView extends AbstractView {

    private final Writer writer;

    public AbstractAtomFeedView(String mimeType) {
        this(mimeType, getWriterFactory().getWriterByMediaType(mimeType));
    }

    public AbstractAtomFeedView(String mimeType, Writer writer) {
        super.setContentType(mimeType);
        this.writer = writer;
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        @SuppressWarnings(value = "unchecked")
        Feed f = (Feed) model.get("feed");
        f.setId(request.getRequestURL().append("?").append(request.getQueryString()).toString());
        response.setContentType(getContentType());
        ServletOutputStream out = response.getOutputStream();
        f.writeTo(writer, out);
    }
}
